{
Traviesadora beta 0.2.0.13.33 (30.11.2008)
LastMod: 20.03.2009 by Prof1983

Copyright 2008, travieorama@googlemail.com

This file is part of the taviesadora project

taviesadora is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

taviesadora is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
}
unit AccountInfoWin;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, IdBaseComponent, IdComponent,jabber,
  IdTCPConnection, IdTCPClient, IdHTTP, Buttons, Avatar, ExtDlgs;

type
  TfrmAccountInfo = class(TForm)
    edtNick: TLabeledEdit;
    imgAvatar: TImage;
    edtEmail: TLabeledEdit;
    edtURL: TLabeledEdit;
    edtBirthday: TLabeledEdit;
    edtCity: TLabeledEdit;
    edtGender: TLabeledEdit;
    memDesc: TMemo;
    Label1: TLabel;
    lbljid: TLabel;
    memtoys: TMemo;
    Label2: TLabel;
    edtFavtoy: TLabeledEdit;
    edtCountry: TLabeledEdit;
    sbtnLoadPic: TSpeedButton;
    OpenPictureDialog1: TOpenPictureDialog;
    pnlProgress: TPanel;
    Bevel1: TBevel;
    lblActivity: TLabel;
    Panel2: TPanel;
    Timer1: TTimer;
    sbtnClearPic: TSpeedButton;
    imgshadow: TImage;
    imgAvatar_nopic: TImage;
    imgAvatar_bgpic: TImage;
    procedure btnCancelClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnOKClick(Sender: TObject);
    procedure sbtnLoadPicClick(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure sbtnClearPicClick(Sender: TObject);
  private
    btnOK: TButton;
    btnCancel: TButton;
  private
    FJabber: TJabber;
    procedure SetJabber(Value: TJabber);
  protected
    procedure DoCreate(); override;
  public
    procedure Init(Jabber: TJabber);
    //property JabberObj: TJabber read FJabber write SetJabber;
  end;

var
  frmAccountInfo: TfrmAccountInfo;

implementation

uses
  TraviesadoraApp;

{$R *.dfm}

{ TfrmAccountInfo }

procedure TfrmAccountInfo.SetJabber(Value: TJabber);
var
  //i: Integer;
  s: string;
  s1: string;
  //avatar: TAvatar;
  //r: TRect;
  UserPr: TUserProfile;
begin
  FJabber := Value;
  UserPr := FJabber.GetMyUserProfile;
  if Assigned(UserPr) then
  begin
    with UserPr do
    begin
      edtNick.Text := GetNick;
      edtEmail.Text := GetEmail;
      edtURL.Text := GetURL;
      edtBirthday.Text := GetBirthday;
      edtCity.Text := GetCity;
      edtGender.Text := GetGender;
      edtFavtoy.Text := GetFavToy;
      edtCountry.Text := GetCountry;
      lbljid.Caption := FJabber.GetUserName+'@'+FJabber.GetServer;
      PaintAvatar(imgAvatar, imgAvatar_bgpic, imgAvatar_nopic, FJabber, nil);
      memtoys.Clear;
      memDesc.Clear;
      s := GetToys+'<br>';
      repeat
        s1 := s;
        delete(s,1,pos('<br>',s)+3);
        delete(s1,pos('<br>',s1),length(s1));
        memtoys.Lines.add(s1);
      until s = '';
      s := GetDesc+'<br>';
      repeat
        s1:=s;
        delete(s,1,pos('<br>',s)+3);
        delete(s1,pos('<br>',s1),length(s1));
        memDesc.Lines.add(s1);
      until s = '';
    end;
  end;
end;

procedure TfrmAccountInfo.btnOKClick(Sender: TObject);
var
  i:integer;
  s:string;
  UserPr:TUserProfile;
begin
  UserPr := FJabber.GetMyUserProfile;
  if Assigned(UserPr) then
  begin
    with UserPr do
    begin
      timer1.Enabled := true;
      pnlprogress.visible := true;
      SetNick(edtNick.Text);
      SetEmail(edtEmail.Text);
      SetURL(edtURL.Text);
      SetBirthday(edtBirthday.Text);
      SetCity(edtCity.Text);
      SetGender(edtGender.Text);
      SetFavToy(edtFavtoy.Text);
      SetCountry(edtCountry.Text);
      s := '';
      for i := 0 to memDesc.Lines.Count-1 do
        s := s+'<br>'+memDesc.Lines[i];
      delete(s,1,4);
      SetDesc(s);
      s := '';
      for i := 0 to memtoys.Lines.Count-1 do
        s := s+'<br>'+memtoys.Lines[i];
      delete(s,1,4);
      SetToys(s);
      UpdateProfile;
    end;
    btnOK.Enabled := false;
    btnOK.Repaint;
  end else
    close;
end;

procedure TfrmAccountInfo.btnCancelClick(Sender: TObject);
begin
  close;
end;

procedure TfrmAccountInfo.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TfrmAccountInfo.sbtnLoadPicClick(Sender: TObject);
var
  Avatar:TAvatar;
  UserPr:TUserProfile;
begin
  UserPr := FJabber.GetMyUserProfile;
  if Assigned(UserPr) then
  begin
    Avatar := UserPr.GetAvatar;
    if Avatar = nil then
    begin
      Avatar := TAvatar.Create;
      UserPr.SetAvatar(Avatar);
    end;
    if OpenPictureDialog1.Execute then
    begin
      Avatar.LoadFromFile(OpenPictureDialog1.FileName);
      PaintAvatar(imgAvatar, imgAvatar_bgpic, imgAvatar_nopic, FJabber, nil);
    end;
  end;
end;

procedure TfrmAccountInfo.sbtnClearPicClick(Sender: TObject);
var
  Avatar:TAvatar;
  UserPr:TUserProfile;
begin
  UserPr := FJabber.GetMyUserProfile;
  if Assigned(UserPr) then
  begin
    Avatar := UserPr.GetAvatar;
    if Assigned(Avatar) then
    begin
      FreeAndNil(Avatar);
      UserPr.SetAvatar(Avatar);
      PaintAvatar(imgAvatar, imgAvatar_bgpic, imgAvatar_nopic, FJabber, nil);
    end;
  end;
end;

procedure TfrmAccountInfo.Timer1Timer(Sender: TObject);
var
  //s: string;
  UserPr: TUserProfile;
begin
  UserPr := FJabber.GetMyUserProfile;
  if Assigned(UserPr) then
  begin
    lblActivity.Caption := 'Uploading profile..';
    if not(UserPr.IsUploading) then
    begin
      TravApp.MyAvatarChanged(Self);
      Close;
    end;
    if (panel2.tag = 0) then
    begin
      panel2.Width := panel2.Width+2;
      if panel2.Width+2 >= Bevel1.Width then
        panel2.Tag := 1;
    end
    else
    begin
      panel2.left := panel2.left+2;
      panel2.Width := panel2.Width-2;
      if panel2.Width <= 0 then
      begin
        panel2.Tag := 0;
        panel2.Left := Bevel1.Left+1;
      end;
    end;
  end
  else
    timer1.Enabled := false;
end;

procedure TfrmAccountInfo.DoCreate();
begin
  inherited;

  btnOK := TButton.Create(Self);
  btnOK.Parent := Self;
  btnOK.Left := 208;
  btnOK.Top := 264;
  btnOK.Width := 75;
  btnOK.Height := 25;
  btnOK.Caption := 'Publish';
  btnOK.Font.Charset := DEFAULT_CHARSET;
  btnOK.Font.Color := clWindowText;
  btnOK.Font.Height := -11;
  btnOK.Font.Name := 'MS Sans Serif';
  btnOK.Font.Style := [];
  btnOK.TabOrder := 10;
  btnOK.TabStop := True;
  btnOK.Default := True;
  btnOK.OnClick := btnOKClick;

  btnCancel := TButton.Create(Self);
  btnCancel.Parent := Self;
  btnCancel.Left := 328;
  btnCancel.Top := 264;
  btnCancel.Width := 75;
  btnCancel.Height := 25;
  btnCancel.Caption := 'Cancel';
  btnCancel.Font.Charset := DEFAULT_CHARSET;
  btnCancel.Font.Color := clWindowText;
  btnCancel.Font.Height := -11;
  btnCancel.Font.Name := 'MS Sans Serif';
  btnCancel.Font.Style := [];
  btnCancel.TabOrder := 11;
  btnCancel.TabStop := True;
  btnCancel.Cancel := True;
  btnCancel.OnClick := btnCancelClick;
end;

procedure TfrmAccountInfo.Init(Jabber: TJabber);
begin
  SetJabber(Jabber);
end;

end.
